<% 
	var headContent = {
	include("/pages/template/head.html"){}
%>
<% include("/pages/includes/ztree.html"){} %>

<script type="text/javascript">
		
	var oper;
	jQuery(function($) {
		// 页面方法
		oper = {
			width : 500,
			height : 500,
			form : document.form1,
			list : function() {
				var url = 'system/department/list';
				this.form.action = url;
				this.form.submit();
			},
			view : function(id) {
				var url = 'system/department/view/'+id;
				var title = '查看组织机构';
				Iframe(url, this.width, this.height, title, false, false, false, EmptyFunc);
			},
			add : function(id) {
				id = id||0;
				var url = 'system/department/add/'+id;
				var title = '添加组织机构';
				Iframe(url, this.width, this.height, title);
			},
			edit : function(id) {
				var url = 'system/department/edit/'+id;
				var title = '修改组织机构';
				Iframe(url, this.width, this.height, title);
			},
			del : function(id) {
				var url = 'system/department/delete/'+id;
				var title = '确认要删除该组织机构信息？';
				Confirm(title, function() {
					form1.action = url;
					form1.submit();
				});
			}
		};
		//显示Menu索引
		showMenu('page_system');
		showMenu('page_department');
	});
	
	//分页
	var paginator = function(page) {
		oper.list();
	};
	
	var zTree;
	var demoIframe;
	var newCount=0;

	function addHoverDom(treeId, treeNode) {
	    var sObj = $("#" + treeNode.tId + "_span");
	    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	    var  addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "'></span>";
	    addStr += "<span class='button edit' id='editBtn_" + treeNode.tId + "'></span>";
	    addStr += "<span class='button remove' id='removeBtn_" + treeNode.tId
	    + "' title='add node' onfocus='this.blur();'></span>";
	    sObj.after(addStr);
	    
	    var addBtn = $("#addBtn_"+treeNode.tId);
	    if (addBtn) addBtn.bind("click", function(){
	    	oper.add(treeNode.id);
	        return false;
	    });
	    
		var editBtn = $("#editBtn_"+treeNode.tId);
	    if (editBtn) editBtn.bind("click", function(){
	    	oper.edit(treeNode.id);
	        return false;
	    });
	    
		var delBtn = $("#removeBtn_"+treeNode.tId);
	    if (delBtn) delBtn.bind("click", function(){
	    	oper.del(treeNode.id);
	        return false;
	    });
	    
	};

	function removeHoverDom(treeId, treeNode) {
	    $("#addBtn_"+treeNode.tId).unbind().remove();
	    $("#removeBtn_"+treeNode.tId).unbind().remove();
	    $("#editBtn_"+treeNode.tId).unbind().remove();
	};

	var setting = {
	    check: {
	        enable: false
	    },
	    view: {
	    	addHoverDom: addHoverDom,
	         removeHoverDom: removeHoverDom,
	        dblClickExpand: false,
	        showLine: true,
	        selectedMulti: false
	    },
	    data: {
	        simpleData: {
	            enable:true,
	            idKey: "id",
	            pIdKey: "pId",
	            rootPId: ""
	        }
	    },
	    callback: {
	        beforeClick: function(treeId, treeNode) {
	            var zTree = $.fn.zTree.getZTreeObj("tree");
	            if (treeNode.isParent) {
	                zTree.expandNode(treeNode);
	                return false;
	            } else {
	                // demoIframe.attr("src",treeNode.file + ".html");
	                return true;
	            }
	        }
	    }
	};

	var zNodes =[
	     <% for(item in departments){ %>
	     	{id:${item.id}, pId:${item.parent_id}, name:"${item.name}", open:true} ${itemLP.index==departments.~size?"":","}
	     <% } %>
	];

	$(document).ready(function(){
	    var t = $("#tree");
	    t = $.fn.zTree.init(t, setting, zNodes);
	    demoIframe = $("#testIframe");
	    demoIframe.bind("load", loadReady);
	    var zTree = $.fn.zTree.getZTreeObj("tree");
	   // zTree.selectNode(zTree.getNodeByParam("id", 101));

	});

	function loadReady() {
	    var bodyH = demoIframe.contents().find("body").get(0).scrollHeight,
	            htmlH = demoIframe.contents().find("html").get(0).scrollHeight,
	            maxH = Math.max(bodyH, htmlH), minH = Math.min(bodyH, htmlH),
	            h = demoIframe.height() >= maxH ? minH:maxH ;
	    if (h < 530) h = 530;
	}
</script>

<%
};

var bodyContent = {
%>

<div class="row" style="margin-right: 0px; margin-left: 0px; ">
	<div class="col-md-2">
	<ul id="tree" class="ztree" style="overflow:auto;"></ul>
	</div>
	<div class="col-md-10">

	<form name="form1" action="" method="post"  class="form-inline" role="form">
		<!-- 菜单 -->
		<% include("/pages/template/menu.html"){} %>

		<div class="tableSearch">
			<%  //查询列表 %>
			<div class="form-group">
					<input class="form-control" type="text" name="attr.name" value="${attr.name!''}" 
						placeholder="请输入名称" />
			</div>
			<button type="button" class="btn btn-default" onclick="oper.list();" name="search">
			 		<span class="glyphicon glyphicon-search"></span> 查 询
			</button>
			<button type="button" class="btn btn-default" onclick="resetForm();">
			 		<span class="glyphicon glyphicon-refresh"></span> 重 置
			</button>
			<button type="button" class="btn btn-default" onclick="oper.add();">
			 		<span class="glyphicon glyphicon-plus"></span> 新 增
			</button>
		</div>
		
		<!-- 数据列表 -->
		<table class="table table-striped table-bordered table-hover">
			<thead>
				<tr>
					<th>序号</th>
					<%  // 列表头部 %>
					<th name="parent_id" class="sorting">上级机构</th> 
					<th name="name" class="sorting">机构名称</th>
					<th name="sort" class="sorting">序号</th> 
					<th name="code" class="sorting">机构编码</th>
					<th name="linkman" class="sorting">联系人</th> 
					<th name="linkman_no" class="sorting">联系人电话</th> 
 					<th>操作</th>
				</tr>
			</thead>
			<tbody>
			<% for(item in page.list){ %>
				<tr>
				<td>${itemLP.index }</td>
					<%  // 列表内容 %>
					<td>${item.parentName!'根节点'}</td> 
					<td>${item.name}</td>
					<td>${item.sort}</td> 
					<td>${item.code}</td>
					<td>${item.linkman}</td> 
					<td>${item.linkman_no}</td> 
				<td>
				<a href="javascript:void(0);" class="btn btn-sm btn-success" onclick="oper.view(${item.id});">查看</a> 
				<a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="oper.edit(${item.id});">修改</a> 
				<a href="javascript:void(0);" class="btn btn-sm btn-danger" onclick="oper.del(${item.id});">删除</a>
				</td>
				</tr>
			<% } %>
			</tbody>
		</table>
		<%include("/pages/includes/paginator.html"){} %>
	</form>
	
</div>
</div>
<%}; %>


<% layout("/pages/template/_layout.html",{head:headContent,body:bodyContent}){ %>
<%} %>